package com.yunxin.customui.service.evnethandlerimpl;

import cn.hutool.core.map.CaseInsensitiveMap;
import com.yunxin.customui.bean.card.CardConfig;
import com.yunxin.customui.myinterface.IEventHandler;
import com.yunxin.customui.mapper.ExecuteMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class AnalyzeDif implements IEventHandler<CardConfig, Map<String,Object>> {

    @Resource
    ExecuteMapper executeMapper;

    @Override
    @Transactional
    public List<EventHandlerResult> eventHandle(CardConfig config, Map<String, Object> param) {
        String text = String.valueOf(param.get("text"));
        String componentId = String.valueOf(param.get("componentId"));
        executeMapper.executeWriteSql("update v_cardconfig set text = '"+text+"' where cardid = '"+componentId+"'");
        List<CaseInsensitiveMap<String, Object>> caseInsensitiveMaps = executeMapper.executeReadSql("select tableid,field,htmlhandle from  v_table_sql_column where tableid = 2011  and field in ('d')");
        for (CaseInsensitiveMap<String, Object> caseInsensitiveMap : caseInsensitiveMaps) {
            String tableId = String.valueOf(caseInsensitiveMap.get("tableid"));
            String field = String.valueOf(caseInsensitiveMap.get("field"));
            String htmlhandle = String.valueOf(caseInsensitiveMap.get("htmlhandle"));
            String[] split = htmlhandle.split(",");
            String oldNumber = split[1];
//            String old  = split[0]+","+split[1];
//            String newHtmlHandle = split[0]+","+text;
            String newHtmlHandle = htmlhandle.replace(oldNumber,text);
            executeMapper.executeWriteSql("update v_table_sql_column set htmlhandle = '"+newHtmlHandle+"' where field = '"+field+"' and tableId = '"+tableId+"'");
        }
        EventHandlerResult re = new EventHandlerResult(ResultEventType.Tip, "成功");
        return Collections.singletonList(re);
    }
}
